/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.ants.basecomponent.config;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.ants.basecomponent.util.PropertiesLoader;
import org.tinygroup.commons.tools.CollectionUtil;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.config.util.ConfigurationUtil;

/**
 * 全局配置类
 * @author lifeng
 */
public class Globals {
	
	/** 默认的编码格式 **/
	public static final String DEFAULT_ENCODING = "UTF-8";
	
	/**
	 * 请求处理时间
	 */
	public static String REQUEST_DEAL_TIME_PARAM = "REQUEST_DEAL_TIME_PARAM";
	
	
	public static final String[] DATE_PATTERNS = { "yyyy", "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyy-MM-dd", "yyyyMMdd", "yyyy/MM/dd",
		"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss" };
	
	/**
	 * 保存全局属性值
	 */
	private static Map<String, String> map = CollectionUtil.createHashMap();
	/**
	 * 属性文件加载对象
	 */
	private static PropertiesLoader propertiesLoader = new PropertiesLoader("application.properties");
	
	//---------需要系统配置的数据---------------------------------
	
	//获得后台管理路劲
	public static String getAdminPath() {
		return getConfig("ANTS_ADMIN_PATH");
	}
	//获得前台管理路劲
	public static String getDefaultViewsPath() {
		return getConfig("ANTS_DEFAULT_VIEWS");
	}
	//cookie的路径
	public static String getCookiePath(){
		return getConfig("cookiePath");
	}
	//cookie的域
	public static String getCookieDomain(){
		return getConfig("cookieDomain");
	}
	//读取配置的缓存使用的序列化的工具
	public static String getCacheSerialization(){
		return getConfig("cache.serialization");
	}
	//网站域名
	public static String getDomain(){
		return getConfig("domain");
	}
	
	public static Map<String,String> initConfig(){
		Map<String,String> propertiesMap = CollectionUtil.createHashMap();
		Properties properties = propertiesLoader.getProperties();
		for(String key : properties.stringPropertyNames()){
			if(!StringUtil.isEmpty(properties.getProperty(key))){
				propertiesMap.put(key, properties.getProperty(key));
			}
		}
		return propertiesMap;
	}
	
	/**
	 * 获取配置
	 */
	public static String getConfig(String key) {
		String value = map.get(key);
		if (value == null){
			map.putAll(ConfigurationUtil.getConfigurationManager().getConfiguration());
			//属性文件中的配置覆盖application.xml的配置
			map.putAll(initConfig());
			value = map.get(key);
		}
		return value;
	}
}
